package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TOrder;
import com.jiai.cloud.entity.TOrderdetails;
import com.jiai.cloud.sys.service.ITOrderdetailsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单详情表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/orderDetails")
public class TOrderdetailsController {
    @Autowired
    private ITOrderdetailsService itOrderdetailsService;
    //增
    @RequestMapping(value="/addOrderdetails", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object addOrderdetails(@RequestBody TOrderdetails tOrderdetails){
        itOrderdetailsService.insert(tOrderdetails);
        return  RestResultGenerator.genSuccessResult("");
    }

    //删
    @RequestMapping(value="/delete/{id}", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object delete(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itOrderdetailsService.deleteById(id);
            return RestResultGenerator.genSuccessResult("");
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }

<<<<<<< Updated upstream
    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itOrderdetailsService.selectById(id);
            return RestResultGenerator.genSuccessResult();
=======
    //查询所有的
    @RequestMapping(value = "/findByAll", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findByAll() throws ServiceException {
        try {
            Page<TOrderdetails> page = new Page<>(1, 20);
            itOrderdetailsService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
>>>>>>> Stashed changes
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

<<<<<<< Updated upstream
    //查
    @RequestMapping(value="/findByAll/{page}", produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public Object findByAll(@PathVariable("page" ) Integer page1) throws ServiceException{
        try {
            Page<TOrderdetails> page = new Page<>(page1, 20);
            itOrderdetailsService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
=======
    //根据订单查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itOrderdetailsService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
>>>>>>> Stashed changes
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    @ResponseBody
    public Object edit(@RequestBody TOrderdetails tOrderdetails)throws ServiceException {
        try {
            itOrderdetailsService.insertOrUpdate(tOrderdetails);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }
}